package com.bluesun.fileSystem;

import java.awt.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;

import static java.lang.System.*;
import static java.nio.file.Files.copy;
import static java.nio.file.Files.move;

public class fileSystem {

    public static String open(String path) { //进入指定目录
        String record = null;
        File f1 = new File(path.toString());
        boolean m = f1.exists();
        if (m == false) {
            out.println("该文件不存在，无法进入");
        } else {


            try {
                Desktop.getDesktop().open(f1);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            String name = f1.getName();
            out.println(name);
            record = f1.getAbsolutePath();
        }
        return record;
    }

    public static String retu(String record) {//返回上一级目录
        File f2 = new File(record.toString());
        String strParentDirectory = f2.getParent();
        File f02 = new File(strParentDirectory);
        try {
            Desktop.getDesktop().open(f02);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String record1 = f02.getAbsolutePath();
        return record1;
    }

    public static String creat(String record, String name) throws IOException {//新建文件
        String a = "\\";
        String sn = record.concat(a).concat(name);
        File f3 = new File(sn);
        boolean fg = f3.createNewFile();
        if (fg == false) {
            System.out.println("该文件已存在，不允许再次创建");
        } else {
            System.out.println("创建成功");
        }
        return sn;
    }

    public static String delete(String record, String name) {//删除文件
        File fis = new File(record.toString());
        String a = "\\";
        String par = fis.getParent();
        String sn = par.concat(a).concat(name);
        File f4 = new File(sn);
        boolean k = f4.delete();
        if (k == false) {
            out.println("该文件不存在，无法删除");
        } else {
            out.println("删除成功");
        }
        return par;
    }

    public static String creatD(String record, String name) {//新建文件夹
        String a = "\\";
        String sn = record.concat(a).concat(name);
        File f5 = new File(sn);
        boolean l = f5.mkdir();
        if (l == false) {
            System.out.println("该文件夹已存在，不允许再次创建");
        } else {
            System.out.println("创建成功");
        }
        return sn;
    }

    public static String mv(String record, String name, String path) throws IOException {//移动文件
        String a = "\\";
        String sn = record.concat(a).concat(name);
        File f6 = new File(sn);
        File f7 = new File(path);
        Files.move(Paths.get(f6.getAbsolutePath()), Paths.get(f7.getAbsolutePath()));
        return path;
    }

    public static String ls(String record) {
        File f8 = new File(record);
        ArrayList<String> array = new ArrayList<>();
        array.add(Arrays.toString(f8.list()));
        out.println(array);
        return record;
    }

    public static String cat(String record, String name) throws IOException {
        String a = "\\";
        String sn = record.concat(a).concat(name);
        FileInputStream f9 = new FileInputStream(sn);
        byte[] bys = new byte[1024];
        int len;
        while ((len = f9.read(bys)) != -1) {
            out.println(new String(bys, 0, len));
        }
        f9.close();
        return sn;
    }

    public static String wr(String record, String name) throws IOException {
        String a = "\\";
        String sn = record.concat(a).concat(name);
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter buf2 = new BufferedWriter(new FileWriter(sn));
        String str = buf.readLine();
        while (!str.equals("YES")) {
            buf2.write(str);
            buf2.newLine();
            str = buf.readLine();
        }

        buf.close();
        buf2.close();
        return sn;
    }

}
